package com.framework.service.common.impl;

import com.framework.common.model.response.R;
import com.framework.common.util.other.NumeralUtil;
import com.framework.common.util.redis.RedisKeyUtil;
import com.framework.common.util.redis.RedisPrefixUtil;
import com.framework.mapper.common.SmsConfigMapper;
import com.framework.model.common.SmsConfig;
import com.framework.model.system.vo.SystemUserVo;
import com.framework.service.base.BaseService;
import com.framework.service.common.SmsConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.service.common.impl.SmsConfigServiceImpl
 * @description 短信配置业务接口实现类
 * @datetime 2024-06-19 14:54:56
 */
@Service("smsConfigServiceImpl")
public class SmsConfigServiceImpl extends BaseService implements SmsConfigService {
    private Logger log = LoggerFactory.getLogger(SmsConfigServiceImpl.class);
    @Autowired
    private SmsConfigMapper smsConfigMapper;

    /**
     * @param id 1 编号
     * @return com.framework.model.common.SmsConfig
     * @title 根据编号查询短信配置
     * @description 根据编号查询短信配置
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    public SmsConfig getByIdParam(Long id) {
        SmsConfig row = new SmsConfig();
        row.setId(id);
        SystemUserVo systemUserVo = getUser();
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        SmsConfig p = smsConfigMapper.selectByParam(row);
        if (p == null) {
            return new SmsConfig();
        }
        return p;
    }

    /**
     * @param row 1 短信配置类
     * @return com.framework.common.model.response.R
     * @title 新增短信配置
     * @description 新增短信配置
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> save(SmsConfig row) {
        SystemUserVo systemUserVo = getUser();
        //查询是否存在默认短信
        SmsConfig queryParam = new SmsConfig();
        queryParam.setDefaultStatus(NumeralUtil.POSITIVE_NINE_THOUSAND_NINE_HUNDRED_AND_NINETY_NINE);
        int count = smsConfigMapper.findPageListCount(queryParam);
        if (row.getDefaultStatus().intValue() != NumeralUtil.POSITIVE_NINE_THOUSAND_NINE_HUNDRED_AND_NINETY_NINE
                && count == NumeralUtil.POSITIVE_ZERO) {
            return R.fail("必须存在一个默认短信配置!");
        }
        if (row.getDefaultStatus().intValue() == NumeralUtil.POSITIVE_NINE_THOUSAND_NINE_HUNDRED_AND_NINETY_NINE
                && row.getEnableStatus().intValue() != NumeralUtil.POSITIVE_ONE) {
            return R.fail("默认必须启用!");
        }

        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setCreateId(userId);
        row.setCreateTime(date);
        row.setOperaterId(userId);
        row.setOperaterTime(date);
        row.setOperaterStatus(NumeralUtil.POSITIVE_ONE);
        //设了默认并且启用的话，先修改
        if (row.getDefaultStatus().intValue() == NumeralUtil.POSITIVE_NINE_THOUSAND_NINE_HUNDRED_AND_NINETY_NINE
                && row.getEnableStatus().intValue() == NumeralUtil.POSITIVE_ONE) {
            SmsConfig update = new SmsConfig();
            update.setDefaultStatus(NumeralUtil.POSITIVE_SEVEN_THOUSAND_SEVEN_HUNDRED_AND_SEVENTY_SEVEN);
            update.setOperaterId(userId);
            update.setOperaterTime(date);
            update.setOperaterStatus(NumeralUtil.POSITIVE_TWO);
            smsConfigMapper.updateByDefaultStatus(update);
        }
        //在新增
        smsConfigMapper.insertSelective(row);
        return R.ok();
    }

    /**
     * @param row 1 短信配置类
     * @return com.framework.common.model.response.R
     * @title 编辑短信配置
     * @description 编辑短信配置
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> edit(SmsConfig row) {
        SystemUserVo systemUserVo = getUser();
        SmsConfig queryParam = new SmsConfig();
        queryParam.setId(row.getId());
        queryParam.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ZERO);
        queryParam = smsConfigMapper.selectByParam(queryParam);
        if (queryParam == null) {
            return R.fail("编辑信息不存在!");
        }
        if (queryParam.getDefaultStatus().intValue() == NumeralUtil.POSITIVE_NINE_THOUSAND_NINE_HUNDRED_AND_NINETY_NINE
                && row.getDefaultStatus().intValue() != queryParam.getDefaultStatus().intValue()) {
            return R.fail("必须存在一个默认短信配置!");
        }
        if (row.getDefaultStatus().intValue() == NumeralUtil.POSITIVE_NINE_THOUSAND_NINE_HUNDRED_AND_NINETY_NINE
                && row.getEnableStatus().intValue() != NumeralUtil.POSITIVE_ONE) {
            return R.fail("默认必须启用!");
        }
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        row.setOperaterStatus(NumeralUtil.POSITIVE_TWO);
        //设了默认并且启用的话，先修改
        if (row.getDefaultStatus().intValue() == NumeralUtil.POSITIVE_NINE_THOUSAND_NINE_HUNDRED_AND_NINETY_NINE
                && row.getEnableStatus().intValue() == NumeralUtil.POSITIVE_ONE
                && row.getDefaultStatus().intValue() != queryParam.getDefaultStatus().intValue()) {
            SmsConfig update = new SmsConfig();
            update.setDefaultStatus(NumeralUtil.POSITIVE_SEVEN_THOUSAND_SEVEN_HUNDRED_AND_SEVENTY_SEVEN);
            update.setOperaterId(userId);
            update.setOperaterTime(date);
            update.setOperaterStatus(NumeralUtil.POSITIVE_TWO);
            smsConfigMapper.updateByDefaultStatus(update);
        }
        //在修改
        smsConfigMapper.updateByPrimaryKeySelective(row);
        return R.ok();
    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 删除短信配置
     * @description 删除短信配置
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> delete(Long id) {
        SmsConfig row = smsConfigMapper.selectByPrimaryKey(id);
        if (row != null && row.getDefaultStatus() == NumeralUtil.POSITIVE_NINE_THOUSAND_NINE_HUNDRED_AND_NINETY_NINE
                && row.getEnableStatus() == NumeralUtil.POSITIVE_ONE) {
            return R.fail("禁止删除默认短信配置!");
        }
        SystemUserVo systemUserVo = getUser();
        row = new SmsConfig();
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setId(id);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        row.setOperaterStatus(NumeralUtil.NEGATIVE_ONE);
        smsConfigMapper.delete(row);
        return R.ok();
    }

    /**
     * @param idList 1 编号集合
     * @return com.framework.common.model.response.R
     * @title 批量删除短信配置
     * @description 批量删除短信配置
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> batchDeleteList(List<Long> idList) {
        SmsConfig row = new SmsConfig();
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setIdList(idList);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        row.setOperaterStatus(NumeralUtil.NEGATIVE_ONE);
        //smsConfigMapper.deleteList(row);
        return R.ok();
    }

    /**
     * @param row 1 短信配置类
     * @return java.util.List<com.framework.model.common.SmsConfig>
     * @title 查询短信配置集合
     * @description 查询短信配置集合
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    public List<SmsConfig> findByList(SmsConfig row) {
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        List<SmsConfig> list = smsConfigMapper.findByList(row);
        return list;
    }

    /**
     * @param row 1 短信配置类
     * @return com.framework.common.model.response.R
     * @title 分页查询短信配置列表
     * @description 分页查询短信配置列表
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    public R<?> findParamPageList(SmsConfig row) {
        SystemUserVo systemUserVo = getUser();
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        int count = smsConfigMapper.findPageListCount(row);
        if (count == NumeralUtil.POSITIVE_ZERO) {
            return R.ok();
        }
        List<SmsConfig> list = smsConfigMapper.findPageList(row);
        return R.ok(list, count);
    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 启用禁用方法
     * @description 启用禁用方法
     * @author 龘鵺
     * @datetime 2024-06-20 20:28:00
     */
    @Override
    public R<?> enable(Long id) {
        String timeKey = RedisKeyUtil.getObjKey(RedisPrefixUtil.PREFIX_STR_SMS_CONFIG_ENABLE_KEY, RedisPrefixUtil.PREFIX_STR_SMS_CONFIG_ENABLE_KEY,
                id.toString());
        long expireNum = redisUtil.getExpire(timeKey);
        if (expireNum > NumeralUtil.POSITIVE_ZERO) {
            return R.fail("操作频繁,请" + expireNum + "秒后重试!");
        }
        SmsConfig queryParam = new SmsConfig();
        SystemUserVo systemUserVo = getUser();
        queryParam.setId(id);
        queryParam = smsConfigMapper.selectByParam(queryParam);
        if (queryParam == null) {
            return R.fail("信息不存在!");
        }
        if (queryParam.getDefaultStatus().intValue() == NumeralUtil.POSITIVE_NINE_THOUSAND_NINE_HUNDRED_AND_NINETY_NINE) {
            return R.fail("默认必须启用!");
        }
        redisUtil.setString(timeKey, NumeralUtil.STRING_POSITIVE_ZERO, NumeralUtil.POSITIVE_FIFTEEN);
        Long userId = systemUserVo.getId();
        Date date = new Date();
        SmsConfig row = new SmsConfig();
        row.setOperaterId(userId);
        row.setOperaterTime(date);
        row.setOperaterStatus(NumeralUtil.POSITIVE_ONE);
        row.setId(queryParam.getId());
        if (queryParam.getEnableStatus().intValue() == NumeralUtil.POSITIVE_ONE) {
            //需要踢出登录，禁用后，后续完善
            row.setEnableStatus(NumeralUtil.POSITIVE_TWO);
        } else {
            row.setEnableStatus(NumeralUtil.POSITIVE_ONE);
        }
        smsConfigMapper.updateByPrimaryKeySelective(row);
        return R.ok();
    }
}